package kr.co.softtrain.glc;

import kr.co.softtrain.glc.control.NavigationBar;
import kr.co.softtrain.glc.control.NavigationBar.OnChildClickListener;
import kr.co.softtrain.glc.control.NavigationBar.controlPosition;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class BaseActivity extends Activity {
	
	public static final int NAVIGATION_CTRL_MENU = 0x2000;
	public static final int NAVIGATION_CTRL_RANKING = 0x2010;
	
	private NavigationBar m_naviBar;
	
	protected int m_nExitType = EXIT_TYPE_NONE;

	protected static final int EXIT_TYPE_NONE = 0;
	protected static final int EXIT_TYPE_YN = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	}
	
	public void setExitTypeAlert(){
		m_nExitType = EXIT_TYPE_YN;
	}
	
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			
			if (m_nExitType == EXIT_TYPE_YN) {
				AlertDialog.Builder alert = new AlertDialog.Builder(this);
				
				String szExitMsg = "terminate GLC";
				alert.setMessage(szExitMsg);
				
				alert.setPositiveButton( "Yes", new DialogInterface.OnClickListener() {
					public void onClick( DialogInterface dialog, int which) {
						dialog.dismiss();
						BaseActivity.this.finish();
					}
				});
	
				alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick( DialogInterface dialog, int which) {
						dialog.dismiss();
					} });
	
				alert.setCancelable(false);
				alert.show();
				
				return true;
			}
		}
		
		return super.onKeyDown(keyCode, event);
	}
	
	protected boolean initNavigationBar(int nResId){
		return initNavigationBar((ViewGroup)findViewById(nResId));
	}
	
	protected boolean initNavigationBar(ViewGroup group){
		
		if(group == null)return false;
		
		m_naviBar = new NavigationBar(this);
		m_naviBar.setBackgroundResource(R.drawable.bg_navi);
		m_naviBar.setOnChildClickListener(m_childItemClick);
		
		group.addView(m_naviBar, 0);
		
		return true;
	}
	
	OnChildClickListener m_childItemClick = new OnChildClickListener() {
		
		@Override
		public void OnChildItemClick(View v) {
			if(m_navikListener != null){
				
				if(!m_navikListener.OnNavigationItemClick(v)){
					return;
				}
			}
			preDefineNavigationEvent(v);
		}
	};
	
	public void setNaviTitle(String szTitle) {
		if (m_naviBar != null) {
			m_naviBar.setTitle(szTitle);
		}
	}
	
	public void setNaviTitle(int nRes) {
		if (m_naviBar != null) {
			m_naviBar.setBI(nRes);
		}
	}
	
	protected void addNavigationChildView(int nId, int nDrawable, controlPosition pos){
		
		ImageView imgButton = new ImageView(this);
		
		imgButton.setBackgroundResource(nDrawable);
		
		//imgButton.setImageResource(nDrawable);
		imgButton.setId(nId);
		
		m_naviBar.addChildControl(imgButton, pos);
	}

	protected void addNavigationChildView(View view, controlPosition pos){
		m_naviBar.addChildControl(view, pos);
	}
	
	private BaseNavigationListener m_navikListener = null; 

	protected void setBaseNavigationListener(BaseNavigationListener listener) {
		m_navikListener = listener;
	}
	
	public interface BaseNavigationListener { 	
		public abstract boolean OnNavigationItemClick(View v); 
	}
	
	final private void preDefineNavigationEvent(View v){
		switch(v.getId()){
			case NAVIGATION_CTRL_MENU:
				break;
				
			case NAVIGATION_CTRL_RANKING:
				break;
		}
		return;
	}
	
	public NavigationBar getNavigationBar() {
		return m_naviBar;
	}

}
